<script setup lang="ts">
import { onLaunch } from '@dcloudio/uni-app'
import { useAppStore } from './stores/app'
import { useUserStore } from './stores/user'
import { getImInfoAPI } from '@/api/my'

// import { TUILogin } from '@tencentcloud/tui-core'

// #ifdef MP-WEIXIN
try {
    const TUIKit = import('./TUIKit')
} catch (e) {
    console.log('[TUIKit]', e)
}
// #endif

uni.loadFontFace({
    family: 'fuiFont',
    source: 'https://pandauke.oss-cn-guangzhou.aliyuncs.com/static/ttf/fui-icon.ttf'
})

// #ifdef APP-PLUS || H5
import { TUIChatKit } from './TUIKit'
TUIChatKit.init()
// #endif

let vueVersion = 2
// #ifdef VUE3
vueVersion = 3
// #endif

// uni.$SDKAppID = 1600019449 // Your SDKAppID
// uni.$userID = 'qwe123' // Your userID
// uni.$userSig =
//     'eJyrVgrxCdYrSy1SslIy0jNQ0gHzM1NS80oy0zLBwoXlqYZGxlCZ4pTsxIKCzBQlK0MzAwMDQ0sTE0uITGpFQWZRKlDc1NTUCCgFES3JzAWJmRtaWhgYGlmaQE3JTAcanOYXYlHllV8R6e8SkBRW4exv6RppXJzkk5Zv4WsaFVjpnxIenm6R7hPqHGmrVAsAJfkxUw__' // Your userSig

const appStore = useAppStore()
const { getUser, token, getImInfo, imInfo: im } = useUserStore()
uni.hideTabBar()

onLaunch(async () => {
    // #ifdef MP-WEIXIN
    const { TUIChatKit } = await require.async('./TUIKit/index')

    TUIChatKit.init()
    // #endif

    await appStore.getConfig()
    // #ifdef H5
    const { status, close, url } = appStore.getH5Config
    if (status == 0) {
        if (close == 1) return (location.href = url)
        uni.reLaunch({ url: '/pages/empty/empty' })
    }
    // #endif
    // await getUser()

    console.log('判断有没有token', token)

    if (!token) return
    // try {
    //     if (!im && token) {
    //         await getImInfo()
    //         const imInfo = im || useUserStore().imInfo
    //
    //         console.log('im登录')
    //         // IM 登录
    //
    //         TUILogin.login({
    //             SDKAppID: imInfo.config.sdkAppId,
    //             userID: imInfo.account.userId,
    //             userSig: imInfo.account.userSig,
    //             // SDKAppID: uni.$SDKAppID,
    //             // userID: uni.$userID,
    //             // userSig: uni.$userSig,
    //             useUploadPlugin: true, // If you need to send rich media messages, please set to true.
    //             framework: `vue${vueVersion}` // framework used vue2 / vue3
    //         })
    //             .then((res) => {
    //                 console.log('im登录成功', res)
    //             })
    //             .catch((err) => {
    //                 console.log('im错误信息', err)
    //             })
    //     }
    // } catch (err) {
    //     console.log(err)
    //     uni.showToast({
    //         title: err + '',
    //         icon: 'none'
    //     })
    // }
})
</script>
<style lang="scss"></style>
